package com.sskj.mine.ui.activity;


import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.AssetBean;
import com.sskj.mine.asset.data.AssetMainBean;
import com.sskj.mine.bean.AccountEnum;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.AccountActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


/**
 * 钱包账户
 */
@Route(path = RConfig.MINE_ACCOUNT)
public class AccountActivity extends BaseActivity<AccountActivityPresenter> {
    @Autowired
    AccountEnum accountEnum;
    @BindView(R2.id.clTop)
    ConstraintLayout clTop;
    @BindView(R2.id.tvAccountTitle)
    TextView tvAccountTitle;
    @BindView(R2.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R2.id.tvCny)
    TextView tvCny;
    @BindView(R2.id.llRecharge)
    LinearLayout llRecharge;
    @BindView(R2.id.llWithdraw)
    LinearLayout llWithdraw;
    @BindView(R2.id.llTransfer)
    LinearLayout llTransfer;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_account;
    }

    @Override
    public AccountActivityPresenter getPresenter() {
        return new AccountActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        ARouter.getInstance().inject(this);
        setTitle(accountEnum.getTitle());


        if (accountEnum == AccountEnum.钱包账户) {
            tvAccountTitle.setText(App.INSTANCE.getString(R.string.mine_accountActivity1));
            clTop.setBackgroundResource(R.mipmap.mine_icon_wallet_account_bg);
            setRightTitle(App.INSTANCE.getString(R.string.mine_accountActivity2), v -> {
                ARouter.getInstance().build(RConfig.TIBI_OTHER_RECORD).navigation();
            });
        }
        if (accountEnum == AccountEnum.法币账户) {
            tvAccountTitle.setText(App.INSTANCE.getString(R.string.mine_accountActivity3));

            clTop.setBackgroundResource(R.mipmap.mine_icon_fabi_account_ng);
            llWithdraw.setVisibility(View.GONE);
            llRecharge.setVisibility(View.GONE);
            setRightTitle(App.INSTANCE.getString(R.string.mine_accountActivity2), v -> {
                ARouter.getInstance().build(RConfig.FABI_PUBLISH_RECORD).navigation();
            });
        }
        if (accountEnum == AccountEnum.币币账户) {
            tvAccountTitle.setText(App.INSTANCE.getString(R.string.mine_accountActivity4));

            clTop.setBackgroundResource(R.mipmap.mine_icon_bibi_account_bg);
            llWithdraw.setVisibility(View.GONE);
            llRecharge.setVisibility(View.GONE);
            setRightTitle(App.INSTANCE.getString(R.string.mine_accountActivity2), v -> {
                ARouter.getInstance().build(RConfig.BIBI_HISTORY_ENTRUST).navigation();
            });
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_account, new SlimInjector<AssetBean>() {
            @Override
            public void onInject(AssetBean data, IViewInjector injector, List payloads) {

                injector
                        .image(R.id.ivCoin, CoinEnum.getByCode(data.getWallet_code()).getRes())
                        .text(R.id.tvName, data.getWallet_code())
                        .text(R.id.tvAvailable, data.getUsable())
                        .text(R.id.tvFrozen, data.getFrost());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.getData(accountEnum.getType() + "");
                mPresenter.getDataMain(accountEnum.getType() + "");
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.getData(accountEnum.getType() + "");
        mPresenter.getDataMain(accountEnum.getType() + "");
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(llRecharge, () -> {
            ARouter.getInstance().build(RConfig.TIBI_RECHARGE).navigation();
        });
        ClickUtil.click(llWithdraw, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkAuth(this, userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkGoogleSms(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.TIBI_WITHDRAW).navigation();
        });
        ClickUtil.click(llTransfer, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_EXCHANGE).withString(Constans.TYPE, accountEnum.getType() + "").navigation();
        });
    }

    public void updateUI(List<AssetBean> data) {
        slimAdapter.updateData(data);
    }

    public void updateHead(AssetMainBean data) {
        if (coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        tvUSDT.setText(data.getGuzhi_usdt() + " USDT");
        tvCny.setText("≈￥" + data.getGuzhi_cny());
    }
}
